package com.atguigu.service_edu.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.CourseOrderVo;
import com.atguigu.service_edu.client.OrderClient;
import com.atguigu.service_edu.entity.EduCourse;
import com.atguigu.service_edu.entity.chapter.ChapterVo;
import com.atguigu.service_edu.entity.frontvo.CourseFrontVo;
import com.atguigu.service_edu.entity.frontvo.CourseWebVo;
import com.atguigu.service_edu.service.EduChapterService;
import com.atguigu.service_edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;


    @RequestMapping("getCourseFrontList/{page}/{limit}")
    public R getCourseFrontList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String,Object> map = eduCourseService.getCourseFrontList(pageCourse,courseFrontVo);
        return R.ok().data(map);
    }

    @GetMapping("getCourseFrontInfo/{courseId}")
    public R getCourseFrontInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程Id，获取课程的基本信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        //根据课程Id，获取课程章节和小结
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);
        //查询用户是否购买过了该课程
        String id = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBuy = orderClient.isBuyCourse(courseId,id);


        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",isBuy);
    }



    @GetMapping("getCourseOrderInfo/{courseId}")
    public CourseOrderVo getCourseOrderInfo(@PathVariable String courseId){
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        BeanUtils.copyProperties(courseWebVo,courseOrderVo);
        return courseOrderVo;
    }







}
